﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Windows.Forms;

namespace Swsk33.SimpleArcEngine.Dialog
{
	public partial class MergeSameDialog : Form
	{
		/// <summary>
		/// 传入地图主控件
		/// </summary>
		private AxMapControl mapControl;

		public MergeSameDialog(AxMapControl mapControl)
		{
			InitializeComponent();
			this.mapControl = mapControl;
		}

		/// <summary>
		/// 刷新字段下拉框的内容
		/// </summary>
		private void setFieldBox()
		{
			fieldBox.Items.Clear();
			IFeatureLayer layer = (IFeatureLayer)mapControl.get_Layer(layerBox.SelectedIndex);
			for (int i = 0; i < layer.FeatureClass.Fields.FieldCount; i++)
			{
				fieldBox.Items.Add(layer.FeatureClass.Fields.Field[i].Name);
			}
			fieldBox.SelectedIndex = 0;
		}

		/// <summary>
		/// 显示一个融合相同字段的功能窗口
		/// </summary>
		public void ShowMergeDialog()
		{
			// 先获取全部图层
			for (int i = 0; i < mapControl.LayerCount; i++)
			{
				layerBox.Items.Add(mapControl.get_Layer(i).Name);
			}
			if (layerBox.Items.Count == 0)
			{
				MessageBox.Show("请先至少加入一个图层！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			layerBox.SelectedIndex = 0;
			setFieldBox();
			ShowDialog();
		}

		/// <summary>
		/// 融合图层相同字段
		/// </summary>
		/// <param name="inputFeatureClass">输入要素</param>
		/// <param name="outputPath">输出路径</param>
		private void featureMerge(IFeatureClass inputFeatureClass, string outputPath)
		{
			ITableName outputTable = new TableNameClass();
			// 获取shapeFile数据工作空间
			IWorkspaceName workspaceName = new WorkspaceNameClass();
			workspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory";
			workspaceName.PathName = outputPath.Substring(0, outputPath.LastIndexOf("\\"));
			// 通过IDatasetName设置输出结果相关参数
			IDatasetName datasetName = (IDatasetName)outputTable;
			datasetName.Name = outputPath.Substring(outputPath.LastIndexOf("\\") + 1);
			datasetName.WorkspaceName = workspaceName;
			// 初始化IBasicGeoprocessor对象
			IBasicGeoprocessor basicGeo = new BasicGeoprocessorClass();
			basicGeo.SpatialReference = mapControl.SpatialReference;
			// 进行融合运算
			ITable resultTable = basicGeo.Dissolve((ITable)inputFeatureClass, false, fieldBox.SelectedItem.ToString(), "Dissolve.Shape", datasetName);
			IFeatureLayer resultLayer = new FeatureLayer();
			resultLayer.FeatureClass = (IFeatureClass)resultTable;
			mapControl.AddLayer(resultLayer);
		}

		/// <summary>
		/// 执行融合
		/// </summary>
		private void doMerge_Click(object sender, EventArgs e)
		{
			if (outputValue.Text.Equals(""))
			{
				MessageBox.Show("输出位置不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			featureMerge(((IFeatureLayer)mapControl.get_Layer(layerBox.SelectedIndex)).FeatureClass, outputValue.Text);
		}

		/// <summary>
		/// 图层选择变化，就获取相应字段
		/// </summary>
		private void layerBox_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			setFieldBox();
		}

		private void setPath_Click(object sender, EventArgs e)
		{
			SaveFileDialog dialog = new SaveFileDialog();
			dialog.Filter = "Shape File|*.shp";
			dialog.Title = "选择保存位置";
			if (dialog.ShowDialog() == DialogResult.OK)
			{
				outputValue.Text = dialog.FileName;
			}
		}
	}
}